$(function () {
    var roomid="";
    listLive(1);

    $('body').on('click', '#btn1, #btn2', function () {
        listLive(1);
    }).on('click', '#live-list-context i.fa-trash', function () {
        var parent = $(this).parents('div:eq(0)');
        icModale(2, '确认关闭选中的直播间吗?', '', function () {
            deleteLive(parent,parent.data('room'));
        })
    }).on('click','.openuser',function(){
        openUser($(this));
    }).on('click',"#copyUrl",function () {
        var roomid = $(this).data('roomid');
        copyUrl(roomid);
    });

    function copyUrl(roomid) {
        var shareUrl_ = $("#shareUrl_"+roomid);
        shareUrl_.select();
        document.execCommand("Copy");
        layer.msg("已复制")
    }

    function openUser(that) {
        //今日直播调起选择直播身份和下载直播客户端
        liverOpenOptionsFn();
        roomid= that.data('roomid');
    }


    //今日直播调起选择直播身份和下载直播客户端
    function liverOpenOptionsFn() {
        var _ele = $('#sysItemLiveTipsEle').val()
        parent.layer.open({
            type: 1,
            area: ['860px', '600px'],
            content: _ele,
            shade: [0.3, '#000'],
            shadeClose: false,
            title: '今日直播',
            success: function (layero, index) {
                layero.find('[data-toggle="tooltip"]').tooltip();
                layero.find("#theTeacherLiveClient").on("click", function () {
                    openInputName(1,roomid,1);
                });
                layero.find("#theTeacherLiveWebPage").on("click", function () {
                    openInputName(1, roomid,2);
                });
                layero.find("#theTaLiveClient").on("click", function () {
                    openInputName(2, roomid,1);
                });
                layero.find("#theTaLiveWebPage").on("click", function () {
                    openInputName(2, roomid,2);
                });
                layero.find("#theHostLiveClient").on("click", function () {
                    openInputName(3, roomid,1);
                });
                layero.find("#theHostLiveWebPage").on("click", function () {
                    openInputName(3, roomid,2);
                });
            }
        });
    }

    function openInputName(type,roomid,openType){
        parent.layer.closeAll();
        if (openType == '1') {
            parent.layer.open({
                content: '进入前需安装直播客户端，如已安装可点击进入即可！'
                ,title: '友情提示'
                , btn: ['进入直播间', '下载客户端']
                , yes: function (index, layero) {
                    toLive(type, roomid, openType);
                }, btn2: function (index, layero) {
                    window.location.href = "https://ftp.3ttech.cn/云中课堂.exe";
                }, cancel: function () {
                }
            });
        } else {
            toLive(type,roomid,openType)
        }
    }

    function toLive(type,roomid,openType){
        var liveUrl = "";
        var _ele;
        _ele = $('#openInputName').val()
        if(type != 3 ){
            $.ajax({
                url: baseloaction + '/admin/liveToV/create/getLiveParam',
                data: {'type': type, 'roomId': roomid, 'selectType': 'liveList'},
                dataType: 'json',
                success: function (result) {
                    if (openType == '1') {
                        liveUrl = "yzkt://quickenterlive&i=" + result.entity.UID + "&k=" + result.entity.safeKey + "&t=" + result.entity.timeStamp + "&r=" + result.entity.classId + "";
                        window.protocolCheck(
                            liveUrl,
                            function () {
                                icModale(0, "客户端软件未安装");
                            }
                        );
                        event.preventDefault ? event.preventDefault() : event.returnValue = false;
                    } else {
                        var url = result.entity.classRoomUrl;
                        $("#url").val(url);
                        $("#toLiveIframe").submit();
                    }
                    parent.layer.closeAll();
                }
            });
        }else{
            var html = '请输入账号';
            parent.layer.open({
                type: 1,
                area: ['240px','180px'],
                skin: 'layui-layer-demo',
                title: '<strong>'+html+'</strong>',
                content: _ele,
                btn: '确定',
                yes: function(index, layero) {
                    var pass = layero.find('#rejectedCause').val();
                    if(pass.length<=0){
                        icModale(0, html);
                        return  ;
                    }
                    if(pass.length>=30){
                        icModale(0, '不能大于30个字符');
                        return  ;
                    }
                    icModale(0, '进入中');
                    $.ajax({
                        url: baseloaction + '/admin/liveToV/create/getLiveParam',
                        data: {'type': type, 'roomId': roomid, 'name':pass,'selectType': 'liveList'},
                        dataType: 'json',
                        success: function (result) {
                            if (result.success == true) {
                                if (openType == '1') {
                                    liveUrl = "yzkt://quickenterlive&i=" + result.entity.UID + "&k=" + result.entity.safeKey + "&t=" + result.entity.timeStamp + "&r=" + result.entity.classId + "";
                                    window.protocolCheck(
                                        liveUrl,
                                        function () {
                                            icModale(0, "客户端软件未安装");
                                        }
                                    );
                                    event.preventDefault ? event.preventDefault() : event.returnValue = false;
                                } else {
                                    var url = result.entity.classRoomUrl;
                                    $("#url").val(url);
                                    $("#toLiveIframe").submit();
                                    parent.layer.closeAll();
                                }
                            }else{
                                icModale(0, result.message);
                            }
                        }
                    });
                    parent.layer.closeAll();
                }
            });
        }
    }


    function deleteLive(obj,room) {
        $.ajax({
            url: baseloaction + '/admin/course/ajaxLiveClose',
            data: {'room_id': room,"type":$(obj).data('service-type')},
            dataType: 'json',
            type: 'post',
            success: function (response) {
                if (response.success) {
                    var _that =obj.parent('div').parent('div').parent('div');
                    obj.find('span').remove();
                    _that.find('.badge').removeClass("badge-info").addClass("badge-danger").text("已关闭");
                    icModale(0,"关闭成功，关闭后直播间不可用");
                } else {
                    icModale(0, response.message);
                }
            },
            error: function () {
                icModale(0, '系统繁忙，请稍候再试');
            }
        })
    }

    function listLive(currentPage) {
        var params = $("#search-form").serializeJSON();
        params.type =$("input[name='type']").val();
        params = JSON.stringify(params);
        ajaxGetListData({
            url: baseloaction + '/admin/course/ajaxLiveList',//请求URL
            contextId: 'live-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: currentPage,//页码
            pageCall: listLive, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-course', //全选框ID
            callback: function () {//数据加载完成回调
                $("#count").text('（共'+total+'个直播）');
            }
        });
    }

});